<template>
  <div>
    
    <div>
      <el-card style="max-width: 900px">
        <template #header>
          <div class="card-header">
            <div>
              <span>书本管理</span><br>
              <el-button type="success">新增</el-button>
              <el-button type="danger">删除</el-button>
            </div>
          </div>
        </template>

        <el-table :data="currentTableData" style="width: 100%">
          <el-table-column prop="imge" label="图片" width="180">
            <template #default="scope">
              <el-image style="width: 100px; height: 100px" :src="scope.row.imge" :fit="fit" />
            </template>
          </el-table-column>
          <el-table-column prop="id" label="编号" width="180" />
          <el-table-column prop="name" label="名字" width="180" />
          <el-table-column prop="price" label="价格" />
          <el-table-column label="数量" width="180">
            <!-- 插槽 -->
            <template #default="scope">
              <el-tag v-if="scope.row.count <= 10" type="danger">库存不足</el-tag>
              <el-tag v-else>{{ scope.row.count }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="author" label="作者" />
          <el-table-column label="操作">
            <template #default="scope">
              <el-button>删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <template #footer>
          <!-- 分页 -->
          <el-pagination
            @current-change="docurrentchange"
            background
            layout="prev, pager, next"
            :total="tableData.length"
            :page-size="pageSize"
          />
        </template>
      </el-card>

      <el-rate v-model="value" allow-half />
      <el-text class="mx-1" type="danger">你好！{{ value }}星</el-text>
    </div>
  </div>
</template>

<script setup>
import { onMounted, ref, computed } from "vue";
import { ElMessage } from 'element-plus';

const tableData = ref([
  { id: 1, name: "自传书", price: "59", count: 10, author: "飒", imge: "https://img0.baidu.com/it/u=940497447,514656619&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083" },
  { id: 2, name: "活着", price: "80", count: 100, author: "余华", imge: "https://img2.baidu.com/it/u=1281341495,1108050852&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1443" },
  { id: 3, name: "自传书", price: "59", count: 10, author: "小枣", imge: "https://img0.baidu.com/it/u=3622372000,1336578847&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667" },
  { id: 4, name: "自传书", price: "59", count: 10, author: "飒", imge: "https://img0.baidu.com/it/u=940497447,514656619&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083" },
  { id: 5, name: "活着", price: "80", count: 100, author: "余华", imge: "https://img2.baidu.com/it/u=1281341495,1108050852&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1443" },
  { id: 6, name: "自传书", price: "59", count: 10, author: "小枣", imge: "https://img0.baidu.com/it/u=3622372000,1336578847&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667" },
  { id: 7, name: "自传书", price: "59", count: 10, author: "飒", imge: "https://img0.baidu.com/it/u=940497447,514656619&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083" },
  { id: 8, name: "活着", price: "80", count: 100, author: "余华", imge: "https://img2.baidu.com/it/u=1281341495,1108050852&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1443" },
  { id: 9, name: "自传书", price: "59", count: 10, author: "小枣", imge: "https://img0.baidu.com/it/u=3622372000,1336578847&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667" },
  { id: 10, name: "自传书", price: "59", count: 10, author: "飒", imge: "https://img0.baidu.com/it/u=940497447,514656619&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083" },
  { id: 11, name: "活着", price: "80", count: 100, author: "余华", imge: "https://img2.baidu.com/it/u=1281341495,1108050852&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1443" },
  { id: 12, name: "自传书", price: "59", count: 10, author: "小枣", imge: "https://img0.baidu.com/it/u=3622372000,1336578847&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667" },
  { id: 13, name: "自传书", price: "59", count: 10, author: "飒", imge: "https://img0.baidu.com/it/u=940497447,514656619&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=1083" },
  { id: 14, name: "活着", price: "80", count: 100, author: "余华", imge: "https://img2.baidu.com/it/u=1281341495,1108050852&fm=253&fmt=auto&app=120&f=JPEG?w=800&h=1443" },
  { id: 15, name: "自传书", price: "59", count: 10, author: "小枣", imge: "https://img0.baidu.com/it/u=3622372000,1336578847&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=667" },
]);

const value = ref(0);
const currentPage = ref(1);
const pageSize = ref(6);

const currentTableData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return tableData.value.slice(start, end);
});

// 分页数据，改变触发
const docurrentchange = (number) => {
  // 消息提示框
  ElMessage.success("当前页:" + number);
  currentPage.value = number;
};

const fit = ref('cover');
const book = () => {
  console.log('book button clicked');
};
</script>    


